//
//  MultiSeleViewController.m
//  HGMM
//
//  Created by huan liu on 2018/10/30.
//  Copyright © 2018年 xwx. All rights reserved.
//

#import "MultiSeleViewController.h"

@interface MultiSeleViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MultiSeleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.selectorPatnArray = [NSMutableArray array];
    
    //设置导航栏按钮
    [self _initNavButtons];
    
    //设置tableview
    [self _initTableView];
    
    self.title = self.multitle;
    
    [self.tableView setEditing:YES animated:YES];
    
}



-(void)_initNavButtons{
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(selectMore:)];
    self.navigationItem.leftBarButtonItem = left;
    
    /*
   UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.tag = 255;
    [button setTitle:@"确定" forState:(UIControlStateNormal)];
   // [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    button.frame = CGRectMake(0, 0, 40, 20);
    [button addTarget:self action:@selector(selectMore:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
     */
    /*
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.tag = 266;
    [button1 setTitle:@"取消" forState:(UIControlStateNormal)];
    //[button1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    button1.frame = CGRectMake(0, 0, 40, 20);
    [button1 addTarget:self action:@selector(selectMore:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
     */
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(dissMissAction:)];
    self.navigationItem.rightBarButtonItem = right;
    
}



- (void)selectMore:(UIButton *)button{

        
        if (self.muilBlock) {
            self.muilBlock(self.selectorPatnArray);
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    
}

-(void)dissMissAction:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



#pragma mark -tableView

-(void)_initTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:kScreenBounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = HGViewsBackgoundColor;
    self.tableView.tableFooterView = [UIView new];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dateArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *Identifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.textLabel.text = self.dateArray[indexPath.row];
    
    return cell;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //选中数据
    [self.selectorPatnArray addObject:self.dateArray[indexPath.row]];
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //从选中中取消
    if (self.selectorPatnArray.count > 0) {
        
        [self.selectorPatnArray removeObject:self.dateArray[indexPath.row]];
    }
    
}


@end
